<template>
  <v-dialog v-model="openUpload" :overlay="false" scrollable persistent max-width="800px" :fullscreen="$vuetify.breakpoint.xsOnly">
      <v-card>
        <v-card-title>
          <span class="headline">{{name}}</span>
        </v-card-title>
        <v-divider></v-divider>
        <v-card-text v-show="updating">
          <v-container grid-list-md>
            <v-layout justify-center>
              <div><v-icon class="loop" x-large >loop</v-icon></div>
            </v-layout>
          </v-container>
        </v-card-text>
        <v-divider v-show="updating"></v-divider>
        <v-card-text>
          <v-container grid-list-md>
            <v-form v-model="valid"  ref="newUpLoadPackageForm">
            <v-layout wrap >
              <v-flex xs12 sm12 md12>
                <v-text-field label="gitCommitId" v-model="commit_hash" :rules="commit_hashRules" :error-messages="msg.commit_hash" required :disabled="handing"></v-text-field>
              </v-flex>
              <v-flex xs12 sm12 md12>
                <input style="width:100%" type="file" @change="getFile" ref="inputer" accept=".jar, .war" :disabled="handing">
              </v-flex>
            </v-layout>
            </v-form>
          </v-container>
          <small>*必填</small>
        </v-card-text>
        <v-divider></v-divider>
        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn color="primary" @click.native="closeUpLoadPackage" small :disabled="handing"><h4>关闭</h4></v-btn>
          <v-btn color="error" @click.native="saveUpLoadPackage" small :disabled="handing"><h4>保存</h4></v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>
</template>

<script>
import {mapActions} from 'vuex'
import {filePost} from '../../utils/axios'
export default {
  name: 'upLoadPackage',
  data () {
    return {
      name: '上传项目包',
      commit_hash: null,
      file: '',
      valid: false,
      updating: false,
      handing: false,
      commit_hashRules: [
        (v) => !!v || '请输入gitId'
      ],
      msg: {
        commit_hash: ''
      }
    }
  },
  watch: {
    openUpload () {
      if (this.openUpload) {
        this.name = '上传项目包'
        this.handing = false
        this.updating = false
        this.$refs.newUpLoadPackageForm.reset()
        this.$refs.inputer.value = ''
        this.file = ''
      } else {
        this.$store.dispatch('listbuild/handlerBuildList', this.$route.params.projectId)
      }
    }
  },
  methods: {
    ...mapActions({
      handlerBuildList: 'handlerBuildList'
    }),
    getFile (e) {
      let inputDOM = this.$refs.inputer
      this.file = inputDOM.files[0]
      let size = Math.floor(this.file.size / 1024)
      if (size > 100000) {
        alert('文件不能大于100M!')
        this.$refs.inputer.value = ''
        this.file = ''
        return false
      }
      let type = this.getFileType(this.file.name)
      if (type !== 'jar' && type !== 'war') {
        alert('非法文件!')
        this.$refs.inputer.value = ''
        this.file = ''
        return false
      }
      // this.$emit('input', this.file)
      // this.fileName = this.file.name
      // this.onChange && this.onChange(this.file, inputDOM.value)
    },
    saveUpLoadPackage () {
      if (this.$refs.newUpLoadPackageForm.validate() && this.file !== undefined && this.file !== '') {
        let param = new FormData()
        param.append('file', this.file)
        param.append('commit_hash', this.commit_hash)
        param.append('Project', this.$route.params.projectId)
        this.name = '上传中，请稍后'
        this.handing = true
        this.updating = true
        filePost('/build/upload_arc/',
          param
        ).then((response) => {
          console.log(response)
          this.$emit('clsUpLoad')
        }).catch((error) => {
          this.handing = false
          this.name = '上传失败'
          if (error.response.data) {
            let datas = error.response.data
            for (var name in datas) {
              this.msg[name] = datas[name]
            }
          }
        })
      }
    },
    getFileType (filePath) {
      var startIndex = filePath.lastIndexOf('.')
      if (startIndex !== -1) {
        return filePath.substring(startIndex + 1, filePath.length).toLowerCase()
      } else {
        return false
      }
    },
    closeUpLoadPackage () {
      this.$emit('clsUpLoad')
    }
  },
  props: {
    openUpload: {
      type: Boolean,
      default: null
    }
  }
}
</script>

<style scoped>
</style>
